package com.google.code.timetrail.gui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import com.google.code.timetrail.backend.Control;
import com.google.code.timetrail.backend.Place;

public class mapPanel extends JPanel{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Control gameControl;
	private BufferedImage background;
	private BufferedImage temp;
	private Image spaceship;


	public mapPanel(String filename1,String filename2,Control gameControl) {
        try {
            background = ImageIO.read(new File(filename1));
            temp = ImageIO.read(new File(filename2));
            spaceship = this.makeColorTransparent(temp, Color.black);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.gameControl=gameControl;
        setOpaque(false);
    }

	 @Override
	    public void paintComponent(Graphics g) {
	       super.paintComponent(g);
	       g.setColor(Color.WHITE);
	       int height=this.getSize().height;
	       int width=this.getSize().width;
	       g.drawImage(background, 0, 0,width,height,null);

	        g.fillRect(0, height/2, width, 4);
	        for(Place p:Place.values())
	        {
	        	if(!p.equals(Place.TRANSIT) && !p.equals(Place.OREGON))
	        	{
	        		if(p.equals(Place.DALLES)){
	    	        	g.drawOval((int)((p.getLocation()/(double)(Place.DALLES.getLocation()))*width)-15, (height/2)-4, 10, 10);
	        		}
	        		else
	        		{
	    	        	g.drawOval((int)((p.getLocation()/(double)(Place.DALLES.getLocation()))*width), (height/2)-4, 10, 10);
	        		}
	        	}
	        }
	        
	        if(gameControl.getCurrentPlace().equals(Place.TRANSIT) ||gameControl.getCurrentPlace().equals(Place.INDEPENDECE)  )
	        {
	        	g.setColor(Color.WHITE);
	        }
	        
	        else
	        	{
	        	g.setColor(Color.RED);
	        	} 
	        
	        if(gameControl.getCurrentPlace().equals(Place.DALLES) || gameControl.getCurrentPlace().equals(Place.OREGON))
	        {
		       g.fillOval((int) ((gameControl.getTime().getTimeTraveled()/1909.0)*width)-15, (height/2)-4, 10, 10);
			   g.drawImage(spaceship, (int) ((gameControl.getTime().getTimeTraveled()/(double)(Place.DALLES.getLocation()))*width)-30, (height/2)-50, 30, 30,null);
	        }
	        if(gameControl.getCurrentPlace().equals(Place.INDEPENDECE))
	        {
		       g.fillOval((int) ((gameControl.getTime().getTimeTraveled()/1909.0)*width), (height/2)-4, 10, 10);
			   g.drawImage(spaceship, (int) ((gameControl.getTime().getTimeTraveled()/(double)(Place.DALLES.getLocation()))*width), (height/2)-50, 30, 30,null);
	        }
	        else
	        {
		        g.fillOval((int) ((gameControl.getTime().getTimeTraveled()/1909.0)*width), (height/2)-4, 10, 10);
				g.drawImage(spaceship, (int) ((gameControl.getTime().getTimeTraveled()/(double)(Place.DALLES.getLocation()))*width), (height/2)-50, 30, 30,null);
	        }
	        
	    }
	 
	 private Image makeColorTransparent (BufferedImage im, final Color color) {
	        ImageFilter filter = new RGBImageFilter() {
	        // the color we are looking for... Alpha bits are set to opaque
	        public int markerRGB = color.getRGB() | 0xFF000000;

	        public final int filterRGB(int x, int y, int rgb) {
	            if ( ( rgb | 0xFF000000 ) == markerRGB ) {
	              // Mark the alpha bits as zero - transparent
	              return 0x00FFFFFF & rgb;
	            } else {
	               // nothing to do
	              return rgb;
	          }
	        }
	      }; 

	      ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
	      return  Toolkit.getDefaultToolkit().createImage(ip);
	    }

}

